package com.letv.android.client.album.controller;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Handler;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.letv.android.client.utils.AudioManagerUtils;
import com.letv.android.young.client.R;
import com.letv.android.client.LetvSDK;
import com.letv.android.client.activity.AlbumPlayRoomActivity;
import com.letv.android.client.controller.AlbumGestureObservable;
import com.letv.android.client.fragment.AlbumRoomPlayFragment;
import com.letv.android.client.view.LetvPanoramaGestureLayout;
import com.letv.android.client.view.LetvPlayGestureLayout;
import com.letv.android.client.view.LetvPlayGestureLayout.LetvPlayGestureCallBack;
import com.letv.business.flow.album.AlbumPlayFlowObservable;
import com.letv.business.flow.album.PlayObservable;
import com.letv.component.player.LetvMediaPlayerControl;
import com.letv.core.db.PreferencesManager;
import com.letv.core.utils.LetvUtils;
import com.letv.core.utils.LogInfo;
import com.letv.core.utils.StatisticsUtils;
import com.letv.core.utils.StringUtils;
import com.letv.core.utils.UIsUtils;
import com.letv.datastatistics.util.PageIdConstant;

import java.util.Observable;
import java.util.Observer;

/**
 * 手势控制器
 * 
 * @author zhuqiao
 *
 */
public class AlbumRoomGestureController implements LetvPlayGestureCallBack, Observer {
    public static final int SCROLL_HALF_TIME = 2 * 60 * 1000;
    public static final int SCROLL_TIME_10 = 10 * 60 * 1000;
    public static final int SCROLL_FULL_TIME = 3 * 60 * 1000;

    private Context mContext;

    private final AudioManager mAudioManager;
    private final AlbumRoomPlayFragment mAlbumPlayFragment;

    /**
     * 手势变化观察者
     */
    private AlbumGestureObservable mGestureObservable;

    /**
     * 播放器之上的手势层
     */
    private LetvPlayGestureLayout mPlayGesture;

    /**
     * 手势 亮度的布局
     */
    private View mBrightnessLayout;
    /**
     * 手势 声音的布局
     */
    private View mVolumeLayout;
    /**
     * 手势 亮度的ProgressBar
     */
    private ProgressBar mBrightnessSeekbar;
    /**
     * 手势 声音的ProgressBar
     */
    private ProgressBar mVolumeSeekbar;
    /**
     * 手势 进度的布局
     */
    private View mProgressLayout;
    /**
     * 手势 当前进度显示
     */
    private TextView mProgressTextView;
    /**
     * 手势 总长度显示
     */
    private TextView mTotalTextView;
    private ProgressBar mGestureProgress;
    private ImageView mProgressIcon;
    private TextView mUnslidableTextView;
    private ImageView mVolumeIcon;

    /**
     * 拦截view
     */
    private View mInterceptView;

    /** 是否上锁 **/
    protected boolean mIsLocked;
    /** 手势操作开始的声音点 **/
    private int mOldVolume;
    /** 手势操作开始的亮度点 **/
    private int mOldBrightness;
    /** 当前亮度 **/
    private int mCurrentBrightness = 0;

    /** 最大亮度 **/
    private int mMaxBrightness;
    /** 最大声音 **/
    private int mMaxVolume;

    /** 手势是否有效，用于在浮层显示后屏蔽手势操作 **/
    private boolean mIsGestureUseful = true;

    /** 点播，手势，上次触摸屏幕时的位置 **/
    private int mLastPos = -1;

    private boolean mIsDisplayFloatLayer = true;

    private boolean mIsLive = false;

    private AlbumPlayRoomActivity mActivity;

    private AudioManagerUtils mAudioManagerUtils;

    /**
     * 改变音量
     *
     * @author zhuqiao
     *
     */
//    public enum VolumnChangeStyle {
//        NONE, UP, DOWN
//    }

    public enum Type {
        LIVE, ALBUM
    }

    public AlbumRoomGestureController(AlbumPlayRoomActivity activity, AlbumRoomPlayFragment albumPlayFragment,
                                      AlbumGestureObservable gestureObservable) {
        mActivity = activity;
        mContext = activity;
        mAlbumPlayFragment = albumPlayFragment;
        mAudioManager = LetvUtils.getAudioManager(mContext);
        mGestureObservable = gestureObservable;
        mAudioManagerUtils = new AudioManagerUtils();
        init();
    }

    /**
     * 设置手势有效性
     * 
     * @param useful
     */
    public void setGestureUseful(boolean useful) {
        mInterceptView.setVisibility(useful ? View.GONE : View.VISIBLE);
    }

    public void enableGestureUseful() {
        mIsGestureUseful = true;
    }

    private void init() {
        if (!(mContext instanceof Activity)) {
            return;
        }
        Activity activity = (Activity) mContext;

        mPlayGesture = (LetvPlayGestureLayout) activity.findViewById(R.id.play_gestrue);
        mBrightnessLayout = activity.findViewById(R.id.brightness_layout);
        mVolumeLayout = activity.findViewById(R.id.volume_layout);
        mBrightnessSeekbar = (ProgressBar) activity.findViewById(R.id.gesture_brightness_progress);
        mVolumeSeekbar = (ProgressBar) activity.findViewById(R.id.gesture_volume_progress);
        mProgressLayout = mPlayGesture.findViewById(R.id.progress_layout);
        mGestureProgress = (ProgressBar) mPlayGesture.findViewById(R.id.gesture_progress);
        mProgressIcon = (ImageView) mPlayGesture.findViewById(R.id.gesture_progress_icon);
        mProgressTextView = (TextView) mPlayGesture.findViewById(R.id.progress);
        mTotalTextView = (TextView) mPlayGesture.findViewById(R.id.total);
        mUnslidableTextView = (TextView) mPlayGesture.findViewById(R.id.unslidable_text);
        mVolumeIcon = (ImageView) activity.findViewById(R.id.gesture_volume_icon);
        mInterceptView = activity.findViewById(R.id.gesture_intercept_view);

        mUnslidableTextView.setVisibility(View.GONE);
        mBrightnessLayout.setVisibility(View.GONE);
        mVolumeLayout.setVisibility(View.GONE);
        mProgressLayout.setVisibility(View.GONE);

        initVolume(getMaxSoundVolume(), getCurSoundVolume());

        mOldBrightness = getScreenBrightness();
        float britness = PreferencesManager.getInstance(mContext).getBritness();
        if (britness != 0) {
            mOldBrightness = (int) (britness * 255);
        }
        initBrightness(getMaxBrightness(), mOldBrightness);

        mPlayGesture.initializeData((float) getCurSoundVolume() / getMaxSoundVolume(), (float) mOldBrightness
                / getMaxBrightness());
        mPlayGesture.setLetvPlayGestureCallBack(this, false);

        // haha
         mPlayGesture.setIsPanorama(LetvPanoramaGestureLayout.PANORAMA_ENABLE);

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                // 等待fragment初始化
                initSoundState(false);
            }
        }, 200);

//        float brightness = LetvApplication.getInstance().getBritness();
        float brightness = LetvSDK.getInstance().getBritness();
        if (brightness != 0) {
            setBrightness(brightness);
        }

        mInterceptView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // 拦截底下视图事件
                // 发送单击通知
                mGestureObservable.notifyObservers(AlbumGestureObservable.ON_CLICK);
            }
        });
    }

    /**
     * 设置直播，禁止进度条滑动
     */
    public void setLive() {
        mIsLive = true;
    }

    // ---------------------- 音量 ----------------------
    /**
     * 控制音量浮层布局是否显示
     * 
     * @param isShow
     */
    public void setVolumeLayoutVisibility(boolean isShow) {
        mVolumeLayout.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    /**
     * 初始化手势声音布局
     */
    private void initVolume(int max, int cur) {
        if (mVolumeSeekbar != null) {
            mMaxVolume = max;
            int curPercent = (int) ((float) cur / mMaxVolume * 100);
            mVolumeSeekbar.setProgress(curPercent);
            setVolumeIcon(curPercent);
        }
    }

    /**
     * 设置音量图片
     * 
     * @param curPercent
     */
    private void setVolumeIcon(int curPercent) {
        if (curPercent == 0) {
//            mVolumeIcon.setImageResource(R.drawable.letv_sound_zero);
            mVolumeIcon.setImageResource(R.drawable.horn);
        } else if (curPercent > 0 && curPercent <= 50) {
//            mVolumeIcon.setImageResource(R.drawable.letv_sound_one);
            mVolumeIcon.setImageResource(R.drawable.horn);
        } else if (curPercent > 50 && curPercent < 100) {
//            mVolumeIcon.setImageResource(R.drawable.letv_sound_two);
            mVolumeIcon.setImageResource(R.drawable.horn);
        } else if (curPercent == 100) {
//            mVolumeIcon.setImageResource(R.drawable.letv_sound_three);
            mVolumeIcon.setImageResource(R.drawable.horn);
        }
    }

    /**
     * 得到最大音量
     */
    public int getMaxSoundVolume() {
        if (mAudioManager == null) {
            return 0;
        }

        return mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    }

    /**
     * 得到当前音量
     */
    public int getCurSoundVolume() {
        if (mAudioManager == null) {
            return 0;
        }

        return mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    }

    /**
     * 初始化音量状态
     */
    private void initSoundState(boolean showSeerbar) {
        int currentValue = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        int maxValue = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        // 发送通知
        mGestureObservable.notifyObservers(new AlbumGestureObservable.VolumeChangeNotify(maxValue, currentValue, VolumnChangeStyle.NONE,
                showSeerbar));
        mOldVolume = currentValue;
    }

    /**
     * 调节音量
     */
    public int setSoundVolume(float value, boolean isShow) {
        int maxValue = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        if (value >= 0 && value <= maxValue) {
            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, (int) value, 0);
            volumeRegulate(isShow, value);
        }

        return maxValue;
    }

    /**
     * 调节声音
     */
    private void volumeRegulate(boolean isShow, float pos) {
        if (isShow) {
            mActivity.showVolume();
            if (mVolumeLayout.getVisibility() != View.VISIBLE) {
                mVolumeLayout.setVisibility(View.VISIBLE);
                // 统计
                LogInfo.LogStatistics("调节声音");
                StatisticsUtils.staticticsInfoPost(mContext, "0", "c66", "1003", 3, null,
                        UIsUtils.isLandscape(mContext) ? PageIdConstant.fullPlayPage : PageIdConstant.halpPlayPage,
                        null, null, null, null, null);
            }
        }

        if (mVolumeSeekbar != null) {
            int curPercent = (int) (pos / mMaxVolume * 100.0);
            mVolumeSeekbar.setProgress(curPercent);
            setVolumeIcon(curPercent);
        }
    }

    // ---------------------- 亮度 ----------------------

    /**
     * 初始化手势亮度布局
     */
    private void initBrightness(int max, int cur) {
        if (mBrightnessSeekbar != null) {
            mMaxBrightness = max;
            int curPercent = (int) ((float) cur / mMaxBrightness * 100);
            mBrightnessSeekbar.setProgress(curPercent);
        }
    }

    /**
     * 得到最大亮度
     */
    private int getMaxBrightness() {
        return 255;
    }

    /**
     * 获得当前屏幕亮度值 0--255
     */
    private int getScreenBrightness() {
        int screenBrightness = 255;
        try {
            screenBrightness = Settings.System.getInt(mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return screenBrightness;
    }

    /**
     * 设置亮度
     */
    private void setBrightness(float value) {
        if (value > 1) {
            value = 1;
        }
        if (value < 0.1f) {// 亮度不能为 0 要不会黑屏
            value = 0.1f;
        }
        Activity activity = (Activity) mContext;

        WindowManager.LayoutParams wl = activity.getWindow().getAttributes();
        wl.screenBrightness = value;
        activity.getWindow().setAttributes(wl);
//        LetvApplication.getInstance().setBritness(value);
        LetvSDK.getInstance().setBritness(value);
    }

    /**
     * 获取当前亮度
     * 
     * @return
     */
    private int getCurrBrightness() {
        Activity activity = (Activity) mContext;

        WindowManager.LayoutParams wl = activity.getWindow().getAttributes();
        float br = wl.screenBrightness;
        if (br < 0) {
            br = 0.1f;
        }
        return (int) (br * 255);
    }

    /**
     * 调节亮度
     */
    private void brightnessRegulate(boolean isShow, int pos) {
        if (isShow) {
            mActivity.showBrightness();
            if (mBrightnessLayout.getVisibility() != View.VISIBLE) {
                mBrightnessLayout.setVisibility(View.VISIBLE);
                // 统计
                LogInfo.LogStatistics("调节亮度");
                StatisticsUtils.staticticsInfoPost(mContext, "0", "c66", "1002", 2, null,
                        UIsUtils.isLandscape(mContext) ? PageIdConstant.fullPlayPage : PageIdConstant.halpPlayPage,
                        null, null, null, null, null);
            }
        }

        if (mBrightnessSeekbar != null) {
            int curPercent = (int) ((float) pos / mMaxBrightness * 100);
            mBrightnessSeekbar.setProgress(curPercent);
        }
    }

    // ---------------------- 进度 ----------------------
    /**
     * 调节进度，默认来自点播，并且不是由用户拖动seekbar引起的浮层联动
     */
    private void progressRegulate(int curPos, int total) {
        progressRegulate(curPos, total, false, Type.ALBUM);
    }

    /**
     * 调节进度
     * 
     * @param curPos
     * @param total
     * @param fromSeekBar 是否是由用户拖动seekbar引起的手势浮层联动
     */
    private void progressRegulate(int curPos, int total, boolean fromSeekBar, Type type) {
        progressRegulate(curPos, total, fromSeekBar, true, type);
    }

    /**
     * 调节进度
     * 
     * @param curPos
     * @param total
     * @param fromSeekBar 如果用户滑动seekbar，则仅需要更改手势浮层；如果用户通过手势控制，则需要回调seekbar
     * @param isSlidable 当前视频资源是否可以调节进度
     * @param type 类型，1.点播、2.直播
     */
    public void progressRegulate(int curPos, int total, boolean fromSeekBar, boolean isSlidable, Type type) {
        mActivity.showSeekProgress();
        if (mProgressLayout != null && mProgressLayout.getVisibility() != View.VISIBLE && mIsDisplayFloatLayer) {
            mProgressLayout.setVisibility(View.VISIBLE);
            if (!fromSeekBar) {
                LogInfo.LogStatistics("调节进度");
                StatisticsUtils.staticticsInfoPost(mContext, "0", "c66", "1001", 1, null,
                        UIsUtils.isLandscape(mContext) ? PageIdConstant.fullPlayPage : PageIdConstant.halpPlayPage,
                        null, null, null, null, null);
            }
        }
        /**
         * 点播格式：00:00，小时被转化为分钟
         */
        if (type == Type.ALBUM) {
            if (mProgressTextView != null) {
                mProgressTextView.setText(StringUtils.stringForTimeNoHour(curPos));
            }
            if (mTotalTextView != null) {
                mTotalTextView.setText(StringUtils.stringForTimeNoHour(total));
            }
            mGestureProgress.setProgress(curPos);
        }
        mGestureProgress.setMax(total);
        /*
         * 前进还是后退
         */
        boolean forward = false;
        /**
         * 点播判断是前进还是后退，可以直接从播放器获取当前位置，并对其进行比较
         */
        if (type == Type.ALBUM) {
            if (mLastPos == -1) {
                int cur = mAlbumPlayFragment.getVideoView().getCurrentPosition();
                forward = cur <= curPos ? true : false;
            } else {
                if (mLastPos != curPos) {
                    forward = mLastPos < curPos ? true : false;
                } else {
                    // is last state
                    forward = mAlbumPlayFragment.getVideoView().getDuration() == curPos;
                }
            }
            mLastPos = curPos;
        }
        /**
         * 提示是否支持进度条的拖动
         */
        if (!isSlidable) {
            if (forward) {
                mUnslidableTextView.setText(R.string.unforward);
            } else {
                mUnslidableTextView.setText(R.string.unbackward);
            }
            mUnslidableTextView.setVisibility(View.VISIBLE);
        } else {
            mUnslidableTextView.setVisibility(View.GONE);
        }
        if (forward) {
            mProgressIcon.setImageResource(R.drawable.letv_kuaijin_normal);
            // isFromLandScroll= true;
        } else {
            mProgressIcon.setImageResource(R.drawable.letv_kuaitui_normal);
            // isFromLandScroll= true;
        }
        /**
         * 如果不是用户拖动进度条引起的手势浮层联动，则需要调用callBack实现进度条和手势浮层的联动
         */
        if (!fromSeekBar) {
            mGestureObservable.notifyObservers(new AlbumGestureObservable.ProgressRegulateNotify(curPos, total, forward));
        }

    }

    // -------------------------------- 手势事件回调 --------------------------------

    @Override
    public void update(Observable observable, Object data) {
        if (data instanceof String) {
            String notify = (String) data;
            if (TextUtils.equals(AlbumPlayFlowObservable.ON_START_FETCHING, notify)) {
                mIsGestureUseful = false;
            } else if (TextUtils.equals(PlayObservable.ON_HEADSET_PLUG, notify)) {
                // 插拔耳机
                initSoundState(true);
            }
        }
    }

    @Override
    public void onDown() {
        mOldVolume = getCurSoundVolume();
        mOldBrightness = getCurrBrightness();
    }

    @Override
    public void onDoubleFingersDown() {
        LogInfo.log("wuxinrong", "双指下滑，缓存到超级电视");
//        startScreenProjection(ScreenProjectionController.SCR_PROJ_TYPE_CACHE);
    }

    @Override
    public void onDoubleFingersUp() {
        LogInfo.log("wuxinrong", "双指上滑，投屏到超级电视");
//        startScreenProjection(ScreenProjectionController.SCR_PROJ_TYPE_PUSH);
    }

    @Override
    public void onSingleTapUp() {
        // 发送单击通知
        LogInfo.log("zhuqiao", "单击");
        mGestureObservable.notifyObservers(AlbumGestureObservable.ON_CLICK);
    }

    @Override
    public void onDoubleTap() {
        // do nothing...
    }

    @Override
    public void onRightScroll(float incremental) {
        // 右边手势移动
        if (!mBrightnessLayout.isEnabled()) {
            return;
        }
        if (!mIsGestureUseful) {
            return;
        }

        mGestureObservable.notifyObservers(AlbumGestureObservable.ON_GESTURE_CHANGE);

        int max = getMaxSoundVolume();
        float newVlaue = mOldVolume + max * incremental;
        if (newVlaue < 0) {
            newVlaue = 0;
        }
        if (newVlaue > max) {
            newVlaue = max;
        }
        setSoundVolume(newVlaue, true);
        // 发送通知
        mGestureObservable.notifyObservers(new AlbumGestureObservable.VolumeChangeNotify(max, (int) newVlaue, VolumnChangeStyle.NONE));
    }

    @Override
    public void onLeftScroll(float incremental) {
        if (!mBrightnessLayout.isEnabled()) {
            return;
        }
        if (!mIsGestureUseful)
            return;

        // 左边手势移动
        int max = getMaxBrightness();
        if (mCurrentBrightness == 0) {
            mOldBrightness = getScreenBrightness();
            float bright = LetvSDK.getInstance().getBritness();
            mOldBrightness = bright == 0 ? mOldBrightness : (int) (bright * max);
            mCurrentBrightness = mOldBrightness;
        }
        int newVlaue = mOldBrightness + (int) Math.floor((int) (max * incremental));
        if (newVlaue < 0) {
            newVlaue = 0;
        }
        if (newVlaue > max) {
            newVlaue = max;
        }

        brightnessRegulate(true, newVlaue);
        setBrightness((float) newVlaue / max);

        mGestureObservable.notifyObservers(AlbumGestureObservable.ON_GESTURE_CHANGE);
    }

    @Override
    public void onMiddleSingleFingerUp() {
        // do nothing
    }

    @Override
    public void onMiddleSingleFingerDown() {
        // do nothing
    }

    @Override
    public void onLandscapeScroll(float incremental) {
        if (!mIsGestureUseful)
            return;

        if(mIsLive) {
            return;
        }

        // 水平手势移动
        int[] result = computeLandscapeScroll(incremental);
        if (result != null && result.length == 2) {
            progressRegulate(result[0], result[1]);
        }
        mGestureObservable.notifyObservers(AlbumGestureObservable.ON_GESTURE_CHANGE);
    }

    @Override
    public void onLandscapeScrollFinish(float incremental) {
        if (!mIsGestureUseful)
            return;

        if(mIsLive) {
            return;
        }
        computeLandscapeScrollFinish(incremental);
        mGestureObservable.notifyObservers(AlbumGestureObservable.ON_GESTURE_CHANGE);
    }

    @Override
    public void onTouchEventUp() {
        mActivity.hideLayout();

        mVolumeLayout.setVisibility(View.GONE);
        mBrightnessLayout.setVisibility(View.GONE);
        mProgressLayout.setVisibility(View.GONE);

        // 通知界面隐藏进度条
        mGestureObservable.notifyObservers(AlbumGestureObservable.ON_TOUCH_EVENT_UP);
        mLastPos = -1;
    }

    @Override
    public void onLongPress() {
        // do nothing...
    }

    @Override
    public void onTouch() {
    }

    /**
     * 计算水平滑动
     */
    private int[] computeLandscapeScroll(float incremental) {
        if (!(mContext instanceof AlbumPlayRoomActivity)) {
            return null;
        }

        AlbumPlayRoomActivity activity = (AlbumPlayRoomActivity) mContext;
        if (activity.getFlow() == null || activity.getAlbumPlayFragment() == null
                || activity.getAlbumPlayFragment().getVideoView() == null) {
            return null;
        }

        // zhuqiao dlna
        LetvMediaPlayerControl videoView = activity.getAlbumPlayFragment().getVideoView();
        if (videoView.isInPlaybackState()) {
            activity.getAlbumPlayFragment().pause();
            activity.getAlbumPlayFragment().getControllerMeditor().pause();
            activity.getAlbumPlayFragment().stopHandlerTime();
            int duration;

            duration = videoView.getDuration();
            /**
             * 10min及以下 视频时长/1 视频时长/1
             * 10min以上 视频时长/10 视频时长/5
             */
            int total = 0;
            if (UIsUtils.isLandscape(mContext)) {
                if (duration > 0) {
                    total = duration > AlbumRoomGestureController.SCROLL_TIME_10 ? duration / 5 : duration;
                }
            } else {
                if (duration > 0) {
                    total = duration > AlbumRoomGestureController.SCROLL_TIME_10 ? duration / 10 : duration;
                }
            }

            int cur;

            cur = videoView.getCurrentPosition();
            int newcur = cur + (int) (incremental * total);
            if (newcur < 0) {
                newcur = 1;
            }

            if (newcur > duration) {
                newcur = duration;
            }

            int[] result = { newcur, duration };
            return result;
        }

        return null;
    }

    /**
     * 水平滑动结束
     * 
     * @param incremental
     */
    public void computeLandscapeScrollFinish(float incremental) {
        if (!(mContext instanceof AlbumPlayRoomActivity)) {
            return;
        }

        AlbumPlayRoomActivity activity = (AlbumPlayRoomActivity) mContext;
        if (activity.getFlow() == null || activity.getAlbumPlayFragment() == null
                || activity.getAlbumPlayFragment().getVideoView() == null) {
            return;
        }

        LetvMediaPlayerControl videoView = activity.getAlbumPlayFragment().getVideoView();
        int duration = videoView.getDuration();
        int total = 0;
        if (UIsUtils.isLandscape(mContext)) {
            if (duration > 0) {
                total = duration > AlbumRoomGestureController.SCROLL_HALF_TIME ? duration / 5 : duration / 2;
            }
        } else {
            if (duration > 0) {
                total = duration > AlbumRoomGestureController.SCROLL_HALF_TIME ? duration / 10 : duration / 2;
            }
        }
        int pos = videoView.getCurrentPosition() + (int) (incremental * total);
        if (pos < 0) {
            pos = 1;
        }
        if (pos > duration) {
            pos = duration;
        }

        // zhuqiao dlna

        LogInfo.log("zhuqiao", "onLandscapeScrollFinish:" + pos);
        videoView.seekTo(pos);
        activity.getAlbumPlayFragment().start();
        activity.getAlbumPlayFragment().getControllerMeditor().start(false);
        activity.getAlbumPlayFragment().startHandlerTime();
        if (activity.getRecommendController() != null) {
            activity.getRecommendController().hideRecommendTipView();
        }
        activity.getAlbumPlayFragment().handSeekFinish(pos);
    }

    @Override
    public int setOneFingertouchInfomation(float begin_x, float begin_y, float end_x, float end_y) {
        // if (mContext instanceof AlbumPlayActivity) {
        // AlbumPlayActivity activity = (AlbumPlayActivity) mContext;
        // if (activity.getAlbumPlayFragment() != null && activity.getAlbumPlayFragment().getVideoView() != null) {
        // return activity.getAlbumPlayFragment().getVideoView()
        // .setOneFingertouchInfomation(begin_x, begin_y, end_x, end_y);
        // }
        // }
        return 0;
    }

    @Override
    public int setTwoFingertouchInfomation(float begin_x0, float begin_y0, float begin_x1, float begin_y1,
            float end_x0, float end_y0, float end_x1, float end_y1) {
        // if (mContext instanceof AlbumPlayActivity) {
        // AlbumPlayActivity activity = (AlbumPlayActivity) mContext;
        // if (activity.getAlbumPlayFragment() != null && activity.getAlbumPlayFragment().getVideoView() != null) {
        // return activity
        // .getAlbumPlayFragment()
        // .getVideoView()
        // .setTwoFingertouchInfomation(begin_x0, begin_y0, begin_x1, begin_y1, end_x0, end_y0, end_x1,
        // end_y1);
        // }
        // }
        return 0;
    }

    @Override
    public int setTwoScale(float scale) {
        // if (mContext instanceof AlbumPlayActivity) {
        // AlbumPlayActivity activity = (AlbumPlayActivity) mContext;
        // if (activity.getAlbumPlayFragment() != null && activity.getAlbumPlayFragment().getVideoView() != null) {
        // return activity.getAlbumPlayFragment().getVideoView().setTwoFingerZoom(scale);
        // }
        // }
        return 0;
    }
    public void onResume() {
        if (mAudioManagerUtils != null)
            mAudioManagerUtils.requestFocus();
    }

    public void onDestroy() {
        if (mAudioManagerUtils != null)
            mAudioManagerUtils.abandonFocus();
    }
}
